package com.googlecode.tudojunto.persistence;

import java.io.Serializable;
import java.util.UUID;

import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;

@MappedSuperclass
public abstract class DomainObject {

	@Transient
	private String uuid;

	public DomainObject() {
		uuid = UUID.randomUUID().toString();
	}

	@Override
	public boolean equals(Object o) {
		if (o == null || !getClass().equals(o.getClass())) {
			return false;
		}
		final DomainObject obj = (DomainObject) o;
		if (this.getId() != null) {
			return this.getId().equals(obj.getId());
		} else {
			return uuid.equals(obj.uuid);
		}
	}

	@Override
	public int hashCode() {
		if (this.getId() != null) {
			return this.getId().hashCode();
		} else {
			return uuid.hashCode();
		}
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public abstract Serializable getId();

}
